import * as types from './mutations-types'

export const setData = async function ({commit, state}, {
   url,
   parameter = {},
   isClose = false,
   isfilter = false,
   objName
} = {}) {
   commit(types.DISPLLAY_LOADING, true)

   if (isClose) {
      commit(types.SET_PARAMETER, {obj: parameter})
   }
   if (isfilter) {
      commit(types.FILTER_DATA, {obj: parameter})
   }
   let params = {}
   let obj = state.WaterfallData
   let parameters = state.parameter
   let filter = state.filterData
   let concat = Object.keys(obj)
   concat = concat.concat(Object.keys(parameters), Object.keys(filter))
   if (concat.length >= 2 && (isClose || isfilter)) {
      commit(types.CLOSE_LAZYLOAD, {isClose: false})
      commit(types.WATERFALL_DATA)
      commit(types.NAVIGATION_BAR_DATA_CLEAR)
   }
   concat.forEach((item, index) => {
      if (obj[item] !== undefined) {
         params[item] = obj[item]
      } else if (parameters[item] !== undefined) {
         params[item] = parameters[item]
      } else if (filter[item] !== undefined) {
         params[item] = filter[item]
      }
   })
   let vm = this._vm
   let data = await vm.$axios.get(url, {params})
   console.log(parameters)
   if (data.data.code === 1 && data.status === 200) {
      commit(types.DETAILS_PAGE_REQUEST_DATA, {data: data.data.result})
      commit(types.DISPLLAY_LOADING, false)
   } else if (data.data.code === 0 && !data.data.success) {
      commit(types.CLOSE_LAZYLOAD, {isClose: true})
      commit(types.DISPLLAY_LOADING, false)
   }
}
